package org.company.tool.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;

/**
 * 获取jdbc连接
 * @ClassName:DBManager.java
 * Title:
 * Description:
 * Copyright:Copyright(c)2012
 * Company:www.si-tech.com.cn
 * @author:hucw
 * @date:2012-12-11
 * @version 1.0
 */
public class DBManager {
	/**
	 * 单例对象
	 */
	private static DBManager instance = null;
	private BasicDataSource dbSource = null;
	
	private DBManager() {
		Properties prop = new Properties();
		
		try {
			prop.load(this.getClass().getResourceAsStream("/ijdbc.properties"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		dbSource = new BasicDataSource();
		dbSource.setUrl(prop.getProperty("db.url"));;
		dbSource.setDriverClassName(prop.getProperty("db.driverClassName"));
		dbSource.setUsername(prop.getProperty("db.username"));
		dbSource.setPassword(prop.getProperty("db.password"));
	}
	
	public static DBManager getInstance() {
		if(instance == null) {
			instance = new DBManager();
		}
		return instance;
	}
	
	/**
	 * @return 返回JDBC连接
	 */
	public Connection getConnection() {
		Connection con = null;
		
		try {
			con = dbSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}
}
